package prj5;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * State information.
 * @author xinruili
 * @version 2022-04-20
 */
public class Data {
    private String stateName;
    private List<RaceStatusData> raceOfStateData;

    /**
     * the data of races.
     * @return the data of races
     */
    public List<RaceStatusData> getRaceOfStateData() {
        return raceOfStateData;
    }

    /**
     * Construction method.
     * @param stateName the name of the state
     * @author xinruili
     * @version 2022-04-20
     */
    public Data(final String stateName) {
        this.stateName = stateName;
        this.raceOfStateData = new ArrayList<>();
    }

    /**
     * Add race.
     * @param raceStatusData race
     * @author xinruili
     * @version 2022-04-20
     */
    public void addRace(final RaceStatusData raceStatusData) {
        this.raceOfStateData.add(raceStatusData);
    }

    /**
     * Return the race souted by CFR.
     * @return RaceSortByCFR
     * @author xinruili
     * @version 2022-04-20
     */
    public List<RaceStatusData> getRaceSortByCFR() {
        this.raceOfStateData.sort(new SortByCFR());
        return this.raceOfStateData;
    }

    /**
     * Return the race souted by Aipha.
     * @return RaceSortByAipha
     * @author xinruili
     * @version 2022-04-20
     */
    public List<RaceStatusData> getRaceSortByAipha() {
        this.raceOfStateData.sort(new SortByAipha());
        return this.raceOfStateData;
    }

    /**
     * Output as required.
     * @author xinruili
     * @version 1.0
     */
    public void showSout() {
        this.getRaceSortByAipha();
        for (RaceStatusData race:this.raceOfStateData) {
            System.out.println(race.getRaceName()
                    + ": " + race.getCaseNumber()
                    + " cases, " + race.getCfr() + "% CFR");
        }
        System.out.println("=====");
        this.getRaceSortByCFR();
        for (RaceStatusData race:this.raceOfStateData) {
            System.out.println(race.getRaceName()
                    + ": " + race.getCaseNumber()
                    + " cases, " + race.getCfr() + "% CFR");
        }
        System.out.println("=====");
    }

    /**
     * Get the name of state.
     * @return the name of state
     * @author xinruili
     * @version 2022-04-20
     */
    public String getStateName() {
        return stateName;
    }
}

/**
 * Comparison method when sorting.
 * @author xinruili
 * @version 2022-04-20
 */
class SortByCFR implements Comparator<RaceStatusData> {
    @Override
    public int compare(final RaceStatusData o1, final RaceStatusData o2) {
        if (o1.getCfr().equals(o2.getCfr())) {
            return o1.getRaceName().compareTo(o2.getRaceName());
        }
        if (Double.valueOf(o1.getCfr()) > Double.valueOf(o2.getCfr())) {
            return -1;
        }
        return 1;
    }
}

/**
 * Comparison method when sorting.
 * @author xinruili
 * @version 2022-04-20
 */
class SortByAipha implements Comparator<RaceStatusData> {
    @Override
    public int compare(final RaceStatusData o1, final RaceStatusData o2) {
        return o1.getRaceName().compareTo(o2.getRaceName());
    }
}
